package com.shuoren.roomtemperaturecloud.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shuoren.roomtemperaturecloud.entity.PageDataInfo;
import com.shuoren.roomtemperaturecloud.entity.ReturnJson;
import com.shuoren.roomtemperaturecloud.repository.HistoryRepository;
import com.shuoren.roomtemperaturecloud.service.FeedbackService;
import com.shuoren.roomtemperaturecloud.util.Constant;

/**
 * @Author: Yann
 * @Date: 2019/1/3 14:20
 */
@Controller
public class IndexController {
	private final static Logger logger = LoggerFactory.getLogger(UserController.class);
	
	@Autowired
	private HistoryRepository historyRepository;
	
	@Autowired
	private FeedbackService feedbackService;
	
	
	/**   
	 * @Author: Yann
	 * @Date: 2019年1月10日
	 * @Description: 获取设备历史
	 * @param:  @param token
	 * @param:  @param d_id
	 * @param:  @param startTime
	 * @param:  @param endTime
	 * @param:  @param pageNo
	 * @param:  @param pageSize
	 * @param:  @return  
	 */
	@ResponseBody
	@PostMapping(value = "/hour/list/device")
	public ReturnJson getHistory(String token, String d_id, long startTime, long endTime, int pageNo, int pageSize) {
		try {
			PageDataInfo pdiDataInfo = historyRepository.getListHistory(d_id, startTime, endTime, pageNo, pageSize);
			//返回结果 
			return new ReturnJson(Constant.Result.SUCCESS_CODE, Constant.Result.SUCCESS_TOAST,pdiDataInfo);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return new ReturnJson(Constant.Result.FAIL_CODE, Constant.Result.FAIL_TOAST, "");
		}
	}
	
	
	/**   
	 * @Author: Yann
	 * @Date: 2019年1月10日
	 * @Description: 保存用户反馈信息
	 * @param:  @param token
	 * @param:  @param userid
	 * @param:  @param context
	 * @param:  @return  
	 */
	@ResponseBody
	@PostMapping(value = "/feedback")
	public ReturnJson feedback(String token,String userid,String context) {
		try {
			Boolean flagBoolean= feedbackService.saveFeedback(userid, context);
			if(flagBoolean) {
				//返回成功结果 
				return new ReturnJson(Constant.Result.SUCCESS_CODE, Constant.Result.SUCCESS_TOAST);
			}else {
				//返回失败结果 
				return new ReturnJson(Constant.Result.FAIL_CODE, Constant.Result.FAIL_TOAST);
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			return new ReturnJson(Constant.Result.FAIL_CODE, Constant.Result.FAIL_TOAST);
		}
	}
}
